﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XZen.Cdn.Client.Blobs;

namespace XZen.Blogger
{
    public static class BlogDataTypeExtn
    {
        public static long? ToNullableLong(this string ID)
        {
            long id = 0;
            long.TryParse(ID, out id);
            return new Nullable<long>(id);
        }
        public static long? ToID(this Article art)
        {
            return ToNullableLong(art.ID);
        }
        public static long? ToID(this Chapter chap)
        {
            return ToNullableLong(chap.ID);
        }
        public static long? ToID(this Issue issue)
        {
            return ToNullableLong(issue.ID);
        }
        public static long? ToID(this Magazine mag)
        {
            return ToNullableLong(mag.ID);
        }
        public static long? ToID(this Blog blog)
        {
            return ToNullableLong(blog.ID);
        }
        public static long? ToID(this Posting posting)
        {
            return ToNullableLong(posting.ID);
        }
        public static long? ToBlogID(this Posting posting)
        {
            return ToNullableLong(posting.BlogID);
        }
        public static IDictionary<string, string> Attributes(this Folder folder)
        {
            var d = XZen.Cdn.Client.div.FromString(folder.Html);
            var items = d.ToDictionary(x => x.name, y => y.value);
            return items;
        }
        public static IDictionary<string, string> Attributes(this File file)
        {
            var d = XZen.Cdn.Client.div.FromString(file.Html);
            var items = d.ToDictionary(x => x.name, y => y.value);
            return items;
        }

        public static string GetAttribute(this File file, string key)
        {
            var d = XZen.Cdn.Client.div.FromString(file.Html);
            var items = d.ToDictionary(x => x.name, y => y.value);
            return GetStringValue(items, key);
        }
        public static string GetAttribute(this Folder folder, string key)
        {
            var d = XZen.Cdn.Client.div.FromString(folder.Html);
            var items = d.ToDictionary(x => x.name, y => y.value);
            return GetStringValue(items, key);
        }

        public static string GetString(this IDictionary<string, string> attrs, string key)
        {
            if (attrs == null) return string.Empty;
            try
            {
                return attrs[key];
            }
            catch
            {

            }
            return string.Empty;
        }
        public static string GetStringValue(this IDictionary<string, string> attrs, string key)
        {
            if (attrs == null) return string.Empty;
            try
            {
                if (attrs[key] == null)
                {
                    return string.Empty;
                }
                else
                {
                    return attrs[key];
                }
            }
            catch
            {

            }
            return string.Empty;
        }
        public static string ToStatus(this int? status)
        {
            FileStatus fs = FileStatus.New;
            try
            { fs = (FileStatus)status.Value; }
            catch { }
            return fs.ToString();
        }
    }
}
